package com.chy.gulimall.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.chy.gulimall.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private String app_id = "2021000117694255";
    // 商户私钥，您的PKCS8格式RSA2私钥
    private String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDMeLwlaswpY+iuhPCF7EuwqN+plsimYX6b3VLBv4yUyUCIzsv9duETHBB31wZd8cKnhKBsi/u3wIHMIU/gaAb8ot06lgaSrOhh1Cxzqya5dhjAoHALR+y5b1SSgdBCzYI68uNKjsFN97eftkoDIce7ZzvkT86px+rkjE4LqpraX4alqVfainaSYiuYCSHpIe9fwxbunjj46k/cuKLluhg2gL6SfuL9CqCLCq+B1OcB3XrYeslv73AO9Hf8qWG5vncWWiXghKH+1/HPXFETE8I232jtjJ7gX1NVOOvWag0v/XvToMwD54fOo6xhy2WwmL4leucX+IvBhL4Iri6/HXc1AgMBAAECggEALXkgZrXkp8PXaJCaKT6O6r/z1fR5tT7zeFh1Yi9mZWSInKRZC4qXrbdNFknU2am9n6HIR7uuBISkmTBDYmXgP+k8iB37Yj1gBPm51ROYLiDMinJj2K5rQCY5Scfi+ChaMR/5Woh7BwrzZGqW9TFbCRuBX1o8XNfgLmnhpYiQncXAfWpW+BWXYDmmNJufMD5FqEWyxq5zYT6ab9K/VjtlTKJgWfseiF+XfEw5/ZiACVHaVYgHMKPjTmP4klPnZoeCKQer7GNuJANRpRQtO0kkODsiF3KNg54vEOVj1J5OvnMxSABjITqMBpUThUNk5In3lYNIoDrnWnm927FG/DR1NQKBgQD/P0BEqJHIDqkwZ28YypGurUnjoEbGKWpDiz3Mhwj6XEtElydCvkgmVfSJVZQNsZW3eiCIP7dsKRM2hW9EfNwgij6bWu1OvxGelekcsvXsneyWXmV+mBofwVpcFe61khWkEo6DWcc7BfTrN3GkSbTE0E4wlnfu/A4j6W9mFpI/dwKBgQDNEyQX16DVna6lVLeMGVI8HvHHVNqeJadK23q/KvO6j4x0jFPXMfz8kluuYyaYwBRFSPzsN84jcEgqbwQgUlBvy5sGFfawrvvifVbZrsfUn/fSmggJtYsT8V+zANpFY6nYb4+sWU0DpNCC6Vi3aLO9e44WSuu1mcL3Qg4r42lhswKBgQDadixvl0eOzt2IDD9OHLKrqq1/IYLxwCivPtHZ6AmLHstdHXIA8hOWnCL8GD7rqX7W1rRZaQ94h39mPraJC8TsJOaI2T4bE/XkobIvWgyaRGBmIs0DmPNjEn5gKnVw+mODVk6bpY+buPFzELU3Q85wVUlW1LI53YnMutMZsV/i6QKBgDVq6fjn6vYZt7dLJPByXBBbAyGoAbY2GqiOGSAYoRoUcEKflus0EzD7MrRvEUuypNUTWQZy1tg2VCXxewKsTlWAlKX2LKcDPYtWmLvlIhpJfPU3uCZHSq3sLsBi1XhDqKc/EA6VyFDNqp6fB8PmmwMdXvqBk3pfTgC4DRbziqyvAoGAbko01PrSrpdccBDB+NOspUrC4umyDVna0vw5AntjSPHP4N3NBPvBzijj4AQECr9ok9BruXVzL7V3k3MNPftXfQtBQRvyyDxNVUeFL0zRHS1iHrt1hDpEpXTzEGJLeYyZEEf+WfzyPWE8BHkeIZ6XeWfTpMfN3rpSzsFc7smsjTg=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAoA6SZOJ8jT6hfVVhpuZ2nwq00ttDkxdfJEHqvjoxm/9HfgRwh3XtEHo6+8c17sE6G9FV89aU2mo/dPK8dhQkW92/8GeQT8J9zR4jUsY6bG7jL5d//ckHOYw6sYm6IGCNONx/wTZzNWj5IQsQf7giCs+OjSmN8iIkFj70uW3z7wXQOQkV/biMKh77in6qofyaAl4s8+UH54k28xhy3v/zXcTlWkJ3uRkQXOnCsDLUXnYqBAQrpCi08arJdwc8HEWCo7cPxv5hXypPMc8ss88dXLvGgeJz+l8UksOiq4kc1TvZE5HUr7ETrt6PWIPQT8c5eM1bH1Ge9JggTLZ9JOdcMwIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private String notify_url = "http://xned5h.natappfree.cc/payed/notify";
    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private String return_url = "http://member.gulimall.com/memberOrder.html";
    // 签名方式
    private String sign_type = "RSA2";
    // 字符编码格式
    private String charset = "utf-8";

    private String timeout = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public String pay(PayVo vo) throws AlipayApiException {
        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);
        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);
        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\""+ timeout +"\","
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();
        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);
        return result;
    }
}
